home *** CD-ROM | disk | FTP | other *** search
- /* (C) Copyright 1991 Dave Fritsche (wb8zxu), All Rights Reserved.
- *
- * Redistribution and use in source and binary forms are permitted for
- * non-commercial use, provided that the above copyright notice and this
- * paragraph are duplicated in all such forms. THIS SOFTWARE IS PROVIDED
- * ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
- * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE.
- */
- /*bdoc
- * Function "INPUTDB"
- *
- * Written: Dave Fritsche
- * Date: July, 1987
- *
- * A function to get input from the console according to an input
- * format pattern. Syntax:
- * input("ctrl-string", result-string)
- * where the "ctrl-string" supplies the format to be used during input
- * and the "result-string" is what was entered before a <CR>. The format
- * characters are:
- * @ == Alphabetic characters only are allowed in this position
- * # == Numeric characters only are allowed in this position
- * | == Numeric characters and "space" are allowed
- * ? == Alpha/Numeric characters only are allowed in this position
- * * == Any character is allowed in this position
- * All other characters are printed literally on the screen as they
- * are encountered in the input process. A couple of special keys
- * are recognized and processed by this routine: the backspace (delete)
- * key, the left-arrow key, and the right arrow key. The arrow keys will
- * move the cursor around the field and will allow over-typing data.
- * If deletion is attempted on a literal ctrl-string character, the cursor
- * is moved backwards until it encounters the first "variable" character
- * to erase. A number of keys listed below will end input and the
- * specific function key that ended input will be returned by the
- * function.
- * Example:
- * key = input("(###) ###-####", phonenum);
- * would only allow numeric input in the positions shown (it is assumed
- * that the programmer would have put an empty "target" field on the
- * screen [like: ( ) - ] and positioned the cursor in front of
- * the first "(" before calling the input function).
- * When "PRINTOPS" is defined below, this function will use whatever
- * is in the "result-string" as a 'seed' for the input function. In
- * other words, the contents of the string will be printed on the
- * screen, input and output pointers will be set to the beginning of this
- * string. This allows the user to escape from the input function,
- * then later come back in and change/update the field.
- * The input function will return a value depending on how the input
- * function is terminated. Return values are:
- * 1 -- The "Up arrow" cursor key was pressed
- * 2 -- The "Down arrow" cursor key was pressed
- * 4 -- The "Shift-Tab" key was pressed
- * 8 -- The "Tab" key was pressed
- * 16 -- Carriage return (or Line feed) was entered
- * 32 -- Escape key pressed
- * 64 -- "F1" key
- * 128 -- "F2" key
- * This capability is enabled by defining "TM220ARROW" (for Ampex-220
- * terminals) or "PCARROW" (for IBM-pc compatibles with ANSI driver)
- * below.
- edoc*/
-
- #include <stdio.h>
- #include <ctype.h>
-
- /* #define UNIX * Define to use UNIX "ugetch()" calls */
- #define PCARROW /* Define to use cursor keys on PC-clone */
- /* #define TM220ARROW * Define to use cursor keys on TM220 */
- #define PRINTOPS /* Define to enable use of previous output */
-
- #ifdef UNIX
- #include "ugetch.h"
- #endif
-
- int inputdb(ips, ops)
- char ips[], ops[];
- {
- int ch, ip, op, ilen, stat, dot, n;
-
- dot = 0;
- ilen = strlen(ips);
- ip = op = 0;
- stat = 0;
-
- #ifdef PRINTOPS
- if ( (strlen(ops) > 0) && (strlen(ops) <= ilen) )
- {
- op = ip = strlen(ops);
- #ifdef CPRINTF
- cprintf("%s", ops);
- #else
- printf("%s", ops);
- #endif
- for (n = 0; n < op; n++)
- if (ops[n] == '.')
- dot += 1;
- }
- #endif
-
- for (n = op; n < ilen; n++)
- ops[n] = ' ';
- ops[ilen] = NULL;
-
- for (n = op; n > 0; n--)
- {
- #ifdef CPRINTF
- cprintf("%c", 8);
- #else
- printf("%c", 8);
- #endif
- op -= 1;
- ip -= 1;
- }
-
- while (stat == 0)
- {
- if ( (ips[ip] != '@') &&
- (ips[ip] != '#') &&
- (ips[ip] != '|') &&
- (ips[ip] != '?') &&
- (ips[ip] != '*') &&
- (ips[ip] != NULL) )
- {
- #ifdef CPRINTF
- cprintf("%c", ips[ip]);
- #else
- printf("%c", ips[ip]);
- #endif
- ops[op] = ips[ip];
- op += 1;
- ip += 1;
- }
- else
- {
- #ifdef UNIX
- ch = ugetch();
- #else
- ch = getch();
- #endif
- if ( (ch == 13) || (ch == 10) )
- {
- stat = 1;
- continue;
- }
-
- else if (ch == 9)
- {
- stat = 3;
- continue;
- }
- #ifdef PCARROW
- else if (ch == 0x1b)
- {
- stat = 2;
- continue;
- }
- else if (ch == 0)
- {
- ch = getch();
- switch(ch)
- {
- case 0x0f:
- stat = 4; /* SHFT-TAB */
- break;
- case 'H':
- stat = 100; /* UP arrow */
- break;
- case 'P':
- stat = 101; /* DOWN arrow */
- break;
- case 'K':
- if (ip <= 0) /* LEFT arrow */
- {
- ip = op = 0;
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- continue;
- }
- while ( (ip > 0) &&
- (ips[ip-1] != '@') &&
- (ips[ip-1] != '#') &&
- (ips[ip-1] != '|') &&
- (ips[ip-1] != '?') &&
- (ips[ip-1] != '*') &&
- (ips[ip-1] != NULL) )
- {
- ip -= 1;
- op -= 1;
- #ifdef CPRINTF
- cprintf("%c", 8);
- #else
- printf("%c", 8);
- #endif
- }
- if (ip <= 0)
- {
- ip = op = 0;
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- continue;
- }
- ip -= 1;
- op -= 1;
- #ifdef CPRINTF
- cprintf("%c", 8);
- #else
- printf("%c", 8);
- #endif
- continue;
- break;
- case 'M': /* RIGHT arrow */
- if (ips[ip] == NULL)
- {
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- continue;
- }
- if ( (ips[ip] != '@') &&
- (ips[ip] != '#') &&
- (ips[ip] != '|') &&
- (ips[ip] != '?') &&
- (ips[ip] != '*') &&
- (ips[ip] != NULL) )
- #ifdef CPRINTF
- cprintf("%c", ips[ip]);
- #else
- printf("%c", ips[ip]);
- #endif
- else
- #ifdef CPRINTF
- cprintf("%c", ops[op]);
- #else
- printf("%c", ops[op]);
- #endif
- ip += 1;
- op += 1;
- while ( (ips[ip] != NULL) &&
- (ips[ip] != '@') &&
- (ips[ip] != '#') &&
- (ips[ip] != '|') &&
- (ips[ip] != '?') &&
- (ips[ip] != '*') )
- {
- #ifdef CPRINTF
- cprintf("%c", ips[ip]);
- #else
- printf("%c", ips[ip]);
- #endif
- ip += 1;
- op += 1;
- }
- if (ips[ip] == NULL)
- {
- while ( (ip > 0) &&
- (ips[ip-1] != '@') &&
- (ips[ip-1] != '#') &&
- (ips[ip-1] != '|') &&
- (ips[ip-1] != '?') &&
- (ips[ip-1] != '*') &&
- (ips[ip-1] != NULL) )
- {
- ip -= 1;
- op -= 1;
- #ifdef CPRINTF
- cprintf("%c", 8);
- #else
- printf("%c", 8);
- #endif
- }
- }
- break;
- case ';':
- stat = 102; /* F1 key */
- break;
- case '<':
- stat = 103; /* F2 key */
- break;
- default:
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- break;
- }
- continue;
- }
- #endif
- #ifdef TM220ARROW
- else if (ch == 0x1b)
- {
- #ifdef UNIX
- if (ugetch() != '[')
- #else
- if (getch() != '[')
- #endif
- #ifdef CPRINTF
- cprintf("%c%c", 7, 7);
- #else
- printf("%c%c", 7, 7);
- #endif
- else
- {
- #ifdef UNIX
- switch (ugetch())
- #else
- switch (getch())
- #endif
- {
- case 'A':
- stat = 100;
- break;
- case 'B':
- stat = 101;
- break;
- case 'C':
- stat = 105;
- break;
- case 'D':
- stat = 104;
- break;
- default:
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- break;
- }
- }
- continue;
- }
- #endif
- else if ( (ch == 8) || (ch == 0x7f) )
- {
- if (ip <= 0)
- {
- ip = op = 0;
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- continue;
- }
- while ( (ip > 0) &&
- (ips[ip-1] != '@') &&
- (ips[ip-1] != '#') &&
- (ips[ip-1] != '|') &&
- (ips[ip-1] != '?') &&
- (ips[ip-1] != '*') &&
- (ips[ip-1] != NULL) )
- {
- ip -= 1;
- op -= 1;
- #ifdef CPRINTF
- cprintf("%c", 8);
- #else
- printf("%c", 8);
- #endif
- }
- if (ip <= 0)
- {
- ip = op = 0;
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- continue;
- }
- ip -= 1;
- op -= 1;
- if ( (ops[op] == '.') && (dot > 0) )
- dot -= 1;
- #ifdef CPRINTF
- cprintf("%c %c", 8, 8);
- #else
- printf("%c %c", 8, 8);
- #endif
- ops[op] = ' ';
- continue;
- }
- switch (ips[ip])
- {
- case NULL:
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- break;
- case '@':
- if (isalpha(ch) == 0)
- {
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- break;
- }
- #ifdef CPRINTF
- cprintf("%c", ch);
- #else
- printf("%c", ch);
- #endif
- ops[op] = ch & 0xff;
- op += 1;
- ip += 1;
- break;
- case '#':
- if (isdigit(ch) == 0)
- {
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- break;
- }
- #ifdef CPRINTF
- cprintf("%c", ch);
- #else
- printf("%c", ch);
- #endif
- ops[op] = ch & 0xff;
- op += 1;
- ip += 1;
- break;
- case '|':
- if ( (isdigit(ch) == 0) &&
- (ch != ' ') &&
- ( (ch != '.') ||
- ((ch == '.') && (dot != 0)
- && (ops[op] != '.')) ) )
- {
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- break;
- }
- if ( (ch == '.') && (ops[op] != '.') )
- dot += 1;
- if ( (ch != '.') && (ops[op] == '.') )
- dot -= 1;
- #ifdef CPRINTF
- cprintf("%c", ch);
- #else
- printf("%c", ch);
- #endif
- ops[op] = ch & 0xff;
- op += 1;
- ip += 1;
- break;
- case '?':
- if (isalnum(ch) == 0)
- {
- #ifdef CPRINTF
- cprintf("%c", 7);
- #else
- printf("%c", 7);
- #endif
- break;
- }
- #ifdef CPRINTF
- cprintf("%c", ch);
- #else
- printf("%c", ch);
- #endif
- ops[op] = ch & 0xff;
- op += 1;
- ip += 1;
- break;
- case '*':
- #ifdef CPRINTF
- cprintf("%c", ch);
- #else
- printf("%c", ch);
- #endif
- ops[op] = ch & 0xff;
- op += 1;
- ip += 1;
- break;
- default:
- break;
- }
- }
- }
- chktxt(ops);
-
- switch(stat)
- {
- case 2:
- return(32);
- break;
- case 3:
- return(8);
- break;
- case 4:
- return(4);
- break;
- case 100:
- return(1);
- break;
- case 101:
- return(2);
- break;
- case 102:
- return(64);
- break;
- case 103:
- return(128);
- break;
- case 104:
- return(4);
- break;
- case 105:
- return(8);
- break;
- default:
- return(16);
- break;
- }
- }
-